import request from '@/utils/request'
import { API_CONFIG } from '@/api/config'

// 查询用户浏览历史列表
export function listBrowseHistory(query) {
  const params = {
    ...query,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/browseHistory/list',
    method: 'get',
    params: params
  })
}

// 根据用户ID查询浏览历史
export function getBrowseHistoryByUserId(userId) {
  return request({
    url: `/mobile/browseHistory/user/${userId}`,
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 查询浏览历史详细
export function getBrowseHistory(browseId) {
  return request({
    url: '/mobile/browseHistory/' + browseId,
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 新增浏览历史
export function addBrowseHistory(data) {
  const requestData = {
    ...data,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/browseHistory',
    method: 'post',
    data: requestData
  })
}

// 修改浏览历史
export function updateBrowseHistory(data) {
  const requestData = {
    ...data,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/browseHistory',
    method: 'put',
    data: requestData
  })
}

// 删除浏览历史
export function delBrowseHistory(browseId) {
  return request({
    url: '/mobile/browseHistory/' + browseId,
    method: 'delete',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 查询用户今日对特定商品的浏览记录
export function getTodayBrowseHistory(userId, productId) {
  return request({
    url: `/mobile/browseHistory/today/${userId}/${productId}`,
    method: 'get',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 批量删除浏览历史
export function delBrowseHistoryBatch(browseIds) {
  return request({
    url: '/mobile/browseHistory/' + browseIds,
    method: 'delete',
    params: {
      orgId: API_CONFIG.ORG_ID
    }
  })
}

// 更新或创建浏览历史记录（用于分段记录浏览时长）
export function upsertBrowseHistory(data) {
  const requestData = {
    ...data,
    orgId: API_CONFIG.ORG_ID
  }
  return request({
    url: '/mobile/browseHistory/upsert',
    method: 'post',
    data: requestData
  })
}